//
//  ViewController.swift
//  YDTestTool
//
//  Created by 赵宏亚 on 2019/11/9.
//  Copyright © 2019 zptec. All rights reserved.
//

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    
    var webview: WKWebView!
    var contentController: WKUserContentController = WKUserContentController()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "云钉绘途"
        
        webview = WKWebView()
        webview.uiDelegate = self
        webview.navigationDelegate = self
        
        // http://192.168.1.107/tp/?appName=yunding
        // http://www.gonghuizhudi.com/?appName=yunding
        let myURL = URL(string: "http://www.gonghuizhudi.com/?appName=yunding")
        let myRequest = URLRequest(url: myURL!)
        webview.load(myRequest)
        self.view = webview
    }
    
    /**
         * WKNavigationDelegate
         */
        /* 页面开始加载时调用 */
        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
            print("页面开始加载")
        }
        
        /* 当内容开始返回时调用 */
        func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
            print("内容开始返回")
        }
        
        /* 页面加载完成后时调用 */
        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        }
        
        /* 页面加载失败时调用 */
        func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
            print("网页加载失败")
        }
        
        //MARK: 跳转支付宝支付方法
        func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
            
            let requrl = navigationAction.request.url
            let scheme = requrl?.scheme
            
            let urlString = String(describing: requrl)
            
            print("requrl ********** \(urlString)")
            
            // 截取itms-apps://itunes.apple.com weixin:
            if (urlString.contains("itunes.apple.com")
                || urlString.contains("itms-services:")
                || urlString.contains("mailto:")
                || urlString.contains("sms:")
                || urlString.contains("tel:")
                || urlString.contains("taobao:")
                || urlString.contains("weixin:")
                || urlString.contains("pinduoduo:")
                || urlString.contains("alipay:")
                || urlString.contains("alipays:")
                || urlString.contains("mqq:")
                || urlString.contains("mqqapi:")) {
                
                let jumpSucc = UIApplication.shared.openURL(requrl!)
                if jumpSucc {
                    print("跳转成功了")
                    decisionHandler(.cancel)
                }
                return
            }
            decisionHandler(.allow)
        }


        


}

